#include "RankLayer.h"

#define PNG_BACK "Play/black_back_V.png"
#define PNG_LABEL "Rank/label_rank.png"
#define PNG_RANK_RED "Rank/rank_red.png"
#define PNG_RANK_YELLOW "Rank/rank_yellow.png"
#define PNG_RANK_BLUE "Rank/rank_blue.png"
#define PNG_RANK_GREEN "Rank/rank_green.png"
#define PNG_RANK_PURPLE "Rank/rank_purple.png"
#define PNG_BOX_2 "Rank/box2.png"
#define PNG_BOX_3 "Rank/box3.png"
#define PNG_BOX_4 "Rank/box4.png"
#define PNG_BOX_5 "Rank/box5.png"

#define TIME_PAUSE 0.8f
#define TIME_MOVE 0.5f

RankLayer::RankLayer()
{
}


RankLayer::~RankLayer()
{
}

Vec2 RankLayer::rankPosition[5];
int RankLayer::rank[5];
Sprite* RankLayer::rankTurtles[5];
Vector<Sprite*> RankLayer::V;

bool RankLayer::init()
{
	Layer::init();

	for ( int i = 0; i < 5; i++)
	{
		RankLayer::rank[i] = i;
	}

	Size size = Director::getInstance()->getVisibleSize();

	Sprite* box21 = Sprite::create(PNG_BOX_2);
	box21->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f)));
	box21->setVisible(false);
	this->addChild(box21, 2);

	Sprite* box22 = Sprite::create(PNG_BOX_2);
	box22->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f * 3)));
	box22->setVisible(false);
	this->addChild(box22, 2);

	Sprite* box23 = Sprite::create(PNG_BOX_2);
	box23->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f * 5)));
	box23->setVisible(false);
	this->addChild(box23, 2);

	Sprite* box24 = Sprite::create(PNG_BOX_2);
	box24->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f * 7)));
	box24->setVisible(false);
	this->addChild(box24, 2);

	Sprite* box31 = Sprite::create(PNG_BOX_3);
	box31->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.13f)));
	box31->setVisible(false);
	this->addChild(box31, 2);

	Sprite* box32 = Sprite::create(PNG_BOX_3);
	box32->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.13f * 2)));
	box32->setVisible(false);
	this->addChild(box32, 2);

	Sprite* box33 = Sprite::create(PNG_BOX_3);
	box33->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.13f * 3)));
	box33->setVisible(false);
	this->addChild(box33, 2);

	Sprite* box41 = Sprite::create(PNG_BOX_4);
	box41->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f * 3)));
	box41->setVisible(false);
	this->addChild(box41, 2);

	Sprite* box42 = Sprite::create(PNG_BOX_4);
	box42->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.065f * 5)));
	box42->setVisible(false);
	this->addChild(box42, 2);

	Sprite* box51 = Sprite::create(PNG_BOX_5);
	box51->setPosition(Vec2(size.width * 0.105f, size.height * (0.84f - 0.13f * 2)));
	box51->setVisible(false);
	this->addChild(box51, 2);

	Sprite* box2s[4] = { box21, box22, box23, box24 };
	Sprite* box3s[3] = { box31, box32, box33 };

	for (int i = 0; i < 4; i++)
	{
		boxes[0][i] = box2s[i];
	}

	boxes[1][0] = box31; boxes[1][1] = box32; boxes[1][2] = box33;
	boxes[2][0] = box41; boxes[2][1] = box42;
	boxes[3][0] = box51;

	for (int i = 0; i < 5; i++)
	{
		RankLayer::rankPosition[i] = Vec2(size.width * 0.105f, size.height * (0.84f - 0.13f * i ));
	}
	
	Sprite* backSprite = Sprite::create(PNG_BACK);
	backSprite->setPosition(Vec2(size.width * 0.105f, size.height * 0.605f));
	this->addChild(backSprite, 0);

	Sprite* label = Sprite::create(PNG_LABEL);
	label->setPosition(Vec2(size.width * 0.105f, size.height * 0.94f));
	this->addChild(label, 0);

	this->rankRed = Sprite::create(PNG_RANK_RED);
	rankRed->setPosition(rankPosition[0]);
	rankRed->setScale(0.52f);
	this->addChild(rankRed, 1);

	this->rankYellow = Sprite::create(PNG_RANK_YELLOW);
	rankYellow->setPosition(rankPosition[1]);
	rankYellow->setScale(0.52f);
	this->addChild(rankYellow, 1);

	this->rankBlue = Sprite::create(PNG_RANK_BLUE);
	rankBlue->setPosition(rankPosition[2]);
	rankBlue->setScale(0.52f);
	this->addChild(rankBlue, 1);

	this->rankGreen = Sprite::create(PNG_RANK_GREEN);
	rankGreen->setPosition(rankPosition[3]);
	rankGreen->setScale(0.52f);
	this->addChild(rankGreen, 1);

	this->rankPurple = Sprite::create(PNG_RANK_PURPLE);
	rankPurple->setPosition(rankPosition[4]);
	rankPurple->setScale(0.52f);
	this->addChild(rankPurple, 1);

	RankLayer::rankTurtles[0] = rankRed;
	RankLayer::rankTurtles[1] = rankYellow;
	RankLayer::rankTurtles[2] = rankBlue;
	RankLayer::rankTurtles[3] = rankGreen;
	RankLayer::rankTurtles[4] = rankPurple;

	V = Vector<Sprite*>();

	return true;
}

void RankLayer::changeRank(int* rankChange, float delay)
{
	int tIndex = *rankChange;
	rankChange++;
	int newRank = *rankChange;
	int oldRank = this->rank[tIndex];

	for (int i = 0; i < 5; i++)
	{
		rankChange++;
		int tI = *rankChange;
		rank[tI] = i;
	}

	rank[tIndex] = newRank;
	rankTurtles[tIndex]->setZOrder(5);
	rankTurtles[tIndex]->runAction(Sequence::create(
		DelayTime::create(delay),
		MoveTo::create(TIME_MOVE, rankPosition[newRank]),
		CallFunc::create([tIndex]
		{
		rankTurtles[tIndex]->setZOrder(1);
		for (int i = 0; i < 5; i++)
		{
			rankTurtles[i]->runAction(
				MoveTo::create(TIME_MOVE, RankLayer::rankPosition[rank[i]]
				));
		}

		}),
	NULL
	));


	//if (newRank < oldRank)
	//{		
	//	for (int i = 0; i < 5; i++)
	//	{
	//		if (newRank - 1 < rank[i] && rank[i] < oldRank)
	//		{
	//			rank[i]++;
	//		}
	//	}
	//}
	//else if (newRank > oldRank)
	//{		
	//	for (int i = 0; i < 5; i++)
	//	{
	//		if (oldRank < rank[i] && rank[i] < newRank + 1)
	//		{
	//			rank[i]--;
	//		}

	//	}
	//}
	//else
	//{
	//	return;
	//}

	//int r[5];
	//for (int i = 0; i < 5; i++)
	//{
	//	r[i] = this->rank[i];
	//}

	//rank[tIndex] = newRank;
	//rankTurtles[tIndex]->setZOrder(5);
	//rankTurtles[tIndex]->runAction(Sequence::create(
	//	MoveTo::create(TIME_MOVE,rankPosition[newRank]),
	//	CallFunc::create([tIndex]
	//	{
	//	rankTurtles[tIndex]->setZOrder(1);
	//	for (int i = 0; i < 5; i++)
	//	{
	//		rankTurtles[i]->runAction(MoveTo::create(TIME_MOVE,RankLayer::rankPosition[rank[i]]));
	//	}

	//	}),
	//	NULL
	//	));
}

void RankLayer::changeOverLap(int* overLap, float delay)
{
	int ol[5];

	for (int i = 0; i < 5; i++)
	{
		ol[i] = *overLap;
		overLap++;
	}

	for (auto sp : V)
	{
		sp->runAction(Sequence::create(
			Hide::create(),
			NULL
			));
	}

	int j = 0; int k = 1;
	int eq = -1;

	if (V.size() > 0)
	{
		V.clear();
	}	

	do
	{
		if (ol[j] == ol[k])
		{
			eq++;
			if (4 == k)
			{
				V.pushBack(this->boxes[eq][j]);
			}
			k++;
		}
		else
		{
			if (eq > -1)
			{
				V.pushBack(this->boxes[eq][j]);
			}

			if (0 == ol[k])
			{
				break;
			}
			j = k; k++; eq = -1;
		}

	} while (k < 5);

	for (auto sp : V)
	{
		sp->runAction(Sequence::create(
			DelayTime::create(delay),
			Show::create(),
			FadeOut::create(0),
			FadeIn::create(0.5f),			
			NULL
			));
	}
}